package cn.shentianlan.service;

import cn.shentianlan.domain.DeviceData;
import cn.shentianlan.domain.DeviceDataForClient;
import cn.shentianlan.service.impl.DeviceDataServiceImpl;
import cn.shentianlan.utils.JsonUtils;
import cn.shentianlan.utils.RedisUtil;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ServletContextAware;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import java.io.BufferedReader;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

@Component
public class ReceiveDeviceData implements ServletContextAware {
    @Override
    public void setServletContext(ServletContext servletContext) {
//        System.out.println("启动1");
        new Thread(new ReceiveData()).start();
    }
}
class ReceiveData implements Runnable {

    private DeviceData deviceData;


    private RedisUtil redisUtil = ApplicationContextUtil.getBean("redisUtils",RedisUtil.class);

    private DeviceDataService deviceDataService = ApplicationContextUtil.getBean("deviceDataServiceImpl", DeviceDataServiceImpl.class);;

    @Override
    public void run() {
        while (true) {
            ServerSocket ss = null;
            Socket s = null;
            InputStream is = null;
            BufferedReader br = null;
            try {
                ss = new ServerSocket(3500);
                //创建一个serversocket其端口与发送端的端口是一样的
                s = ss.accept();
                //侦听并接受到此套接字的连接，返回一个socket对象
                is = s.getInputStream();
                //获取到输入流
                byte[] buf = new byte[1024];
                //接收收到的数据
                int line = 0;
//                System.out.println("执行服务端程序");
                DecimalFormat df = new DecimalFormat( "0.00 ");
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd");
                Random random = new Random();


                while ((line = is.read(buf)) != -1) {
//                    System.out.println(new String(buf, 0, line));
                    DeviceDataForClient deviceDataForClient = JsonUtils.jsonToPojo(new String(buf, 0, line), DeviceDataForClient.class);

                    deviceData = new DeviceData(deviceDataForClient.getGd_dev_O2().toString(),
                            df.format(random.nextDouble()*10),
                            df.format(random.nextDouble()*20),
                            simpleDateFormat.format(new Date()),
                            deviceDataForClient.getGd_dev_Rh().toString(),
                            deviceDataForClient.getGd_dev_TEMP().toString()
                    );
                    String ID = deviceDataForClient.getID();
                    //判断是否存在该设备
                    Object table = deviceDataService.getTable(ID);
                    if (table==null){
                        deviceDataService.createTable(ID);
                    }
                    try {
                        redisUtil.rightPush(ID+":"+simpleDateFormat2.format(new Date()),deviceData);
                    }catch (Exception e){
                        e.printStackTrace();
                    }finally {
                        deviceDataService.addDeviceData(ID,deviceData);
                    }
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                try {
                    is.close();
                    s.close();
                    ss.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
